﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ShevaEngine.Core.Modules.Graphics;
using System;

namespace ShevaEngine2.Tools
{
    /// <summary>
    /// Quad.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Quad : UserIndexedPrimitivesDrawCall<VertexPositionNormalTexture>
    {
        /// <summary>Origin.</summary>
        public Vector3 Origin;
        /// <summary>Upper left.</summary>
        public Vector3 UpperLeft;
        /// <summary>Lower left.</summary>
        public Vector3 LowerLeft;
        /// <summary>Upper right.</summary>
        public Vector3 UpperRight;
        /// <summary>Lower right.</summary>
        public Vector3 LowerRight;
        /// <summary>Normal.</summary>
        public Vector3 Normal;
        /// <summary>Up.</summary>
        public Vector3 Up;
        /// <summary>Left.</summary>
        public Vector3 Left;        

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="normal"></param>
        /// <param name="up"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public Quad(Vector3 origin, Vector3 normal, Vector3 up, float width, float height)
            : base(PrimitiveType.TriangleList, new VertexPositionNormalTexture[4], new Int32[6], 2)
        {
            this.Origin = origin;
            this.Normal = normal;
            this.Up = up;

            // Calculate the quad corners
            Left = Vector3.Cross(normal, Up);
            Vector3 uppercenter = (Up * height / 2) + origin;
            UpperLeft = uppercenter + (Left * width / 2);
            UpperRight = uppercenter - (Left * width / 2);
            LowerLeft = UpperLeft - (Up * height);
            LowerRight = UpperRight - (Up * height);
            
            // Fill in texture coordinates to display full texture
            // on quad
            Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f);
            Vector2 textureUpperRight = new Vector2(1.0f, 0.0f);
            Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f);
            Vector2 textureLowerRight = new Vector2(1.0f, 1.0f);

            // Provide a normal for each vertex
            for (int i = 0; i < Vertices.Length; i++)
            {
                Vertices[i].Normal = Normal;
            }

            // Set the position and texture coordinate for each
            // vertex
            this.Vertices[0].Position = LowerLeft;
            this.Vertices[0].TextureCoordinate = textureLowerLeft;
            this.Vertices[1].Position = UpperLeft;
            this.Vertices[1].TextureCoordinate = textureUpperLeft;
            this.Vertices[2].Position = LowerRight;
            this.Vertices[2].TextureCoordinate = textureLowerRight;
            this.Vertices[3].Position = UpperRight;
            this.Vertices[3].TextureCoordinate = textureUpperRight;

            // Set the index buffer for each vertex, using
            // clockwise winding
            this.Indices[0] = 0;
            this.Indices[1] = 1;
            this.Indices[2] = 2;
            this.Indices[3] = 2;
            this.Indices[4] = 1;
            this.Indices[5] = 3;
        }
    }
}
